package com.gnerv.plug.discuss;

import com.intellij.codeInsight.daemon.GutterIconNavigationHandler;
import com.intellij.codeInsight.daemon.LineMarkerInfo;
import com.intellij.codeInsight.daemon.LineMarkerProvider;
import com.intellij.codeInsight.daemon.LineMarkerProviderDescriptor;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.markup.GutterIconRenderer;
import com.intellij.openapi.editor.markup.MarkupEditorFilter;
import com.intellij.openapi.editor.markup.MarkupEditorFilterFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiIdentifier;
import com.intellij.psi.PsiKeyword;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiReference;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.util.Function;
import com.intellij.util.Query;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.Collection;
import java.util.List;

public class DiscussLineMarkerProvider implements LineMarkerProvider {

    @Override
    public @Nullable LineMarkerInfo getLineMarkerInfo(@NotNull PsiElement element) {

        if (element instanceof PsiClass) {
            System.out.println("PsiClass");
            int textOffset = element.getTextOffset();
            int textLength = element.getTextLength();
            System.out.print(element.getText());
            System.out.print(" " + textOffset);
            System.out.println(" " + textLength);
        } else if (element instanceof PsiKeyword) {
            System.out.println("PsiKeyword");
            int textOffset = element.getTextOffset();
            int textLength = element.getTextLength();
            System.out.print(element.getText());
            System.out.print(" " + textOffset);
            System.out.println(" " + textLength);
        } else if (element instanceof PsiIdentifier) {
            System.out.println("PsiIdentifier");
            int textOffset = element.getTextOffset();
            int textLength = element.getTextLength();
            System.out.print(element.getText());
            System.out.print(" " + textOffset);
            System.out.println(" " + textLength);
        } else if (element instanceof PsiMethod) {
            System.out.println("PsiMethod");
            int textOffset = element.getTextOffset();
            int textLength = element.getTextLength();
            System.out.print(element.getText());
            System.out.print(" " + textOffset);
            System.out.println(" " + textLength);
        } else {
            System.out.println("else");
            int textOffset = element.getTextOffset();
            int textLength = element.getTextLength();
            System.out.print(element.getText());
            System.out.print(" " + textOffset);
            System.out.println(" " + textLength);
        }


        return new RunLineMarkerInfo(element, new TextRange(0, 0), AllIcons.FileTypes.Java, t -> "123123");
//        if(lineNumber == 3){
//            return new RunLineMarkerInfo(element, AllIcons.FileTypes.Java, t -> "123123");
//        }
//        return null;
    }

    @Override
    public void collectSlowLineMarkers(@NotNull List<PsiElement> elements, @NotNull Collection<LineMarkerInfo> result) {

    }

    static class RunLineMarkerInfo extends LineMarkerInfo<PsiElement> {

        RunLineMarkerInfo(PsiElement element, TextRange range, Icon icon, Function<PsiElement, String> tooltipProvider) {
            super(element, range, icon, tooltipProvider, null, GutterIconRenderer.Alignment.CENTER);
        }

        RunLineMarkerInfo(PsiElement element, Icon icon, Function<PsiElement, String> tooltipProvider) {
            super(element, element.getTextRange(), icon, tooltipProvider, null, GutterIconRenderer.Alignment.CENTER);
        }

        @Override
        public GutterIconRenderer createGutterRenderer() {
            return new LineMarkerGutterIconRenderer<PsiElement>(this) {
                @Override
                public AnAction getClickAction() {
                    return null;
                }

                @Override
                public boolean isNavigateAction() {
                    return true;
                }

            };
        }

        @NotNull
        @Override
        public MarkupEditorFilter getEditorFilter() {
            return MarkupEditorFilterFactory.createIsNotDiffFilter();
        }
    }

}
